#include "Display.h"
#include "global.h"
#include "lcd.h"
#include "instrument.h"
#include "rtc.h"
#include "hourmeter.h"
#include "bdi.h"
#include "Key.h"
#include<math.h>
#include <stdlib.h>
#include "string.h"
#include "AnalogInput.h"
U16 welcom_mode_counter;
U16 lcd_test_counter;
U16 proinfo_mode_counter;
U16 Disp_LiftErrCode[4];
U16 Disp_TracErrCode[4];
U16 Disp_SteerErrCode[4];
U16 Disp_DhsbErrCode[4];
U16 Init_flag = 0;
U16 Err_Code_Hide_flag;
U16 Err_Code_Exist_flag;
U16 Err_Oil_Hide_flag;
U16 Err_Oil_Exist_flag;
U16 Err_Temp_Hide_flag;
U16 Err_Temp_Exist_flag;
U16 OilErrCodeDealFlag = TRUE;
U16 TempurateErrCodeDealFlag = TRUE;
U16 MaintErrCodeDealFlag = TRUE;
U16 BatCutoffDealFlag = TRUE;
U16 Err_Maint_Hide_flag = FALSE;
U16 Err_Maint_Exist_flag = TRUE;
U16 Err_BatCutoff_Hide_flag = FALSE;
U16 Err_BatCutoff_Exist_flag = TRUE;
U16 Err_Batlow_Hide_flag = FALSE;
U16 Err_Batlow_Exist_flag = TRUE;
U16 BatLowDealFlag = TRUE;
U8 Current_Bat_Soc = 0;
U8 Battary_type;
U8 Li_BattCon = 0;
U32 pre_DigicodeArray1Value;
U32 pre_DigicodeArray2Value;
U32 pre_DigicodeArray3Value;
U32 pre_DigicodeArray4Value;
U32 pre_DigicodeArray5Value;
U32 pre_DigicodeArray6Value;
U32 pre_DigicodeArray7Value;
U32 pre_DigicodeArray8Value;
U32 Test_step_counter = 0;
U8 EEPROM_test_val = 0;
U8 EEPROM_readback_flag = 0;
/**
* @brief	display task
* @param	none
* @note     none
* @retval   none
*/
void display_task(void const *argument)
{
	LCD_init();
	while(TRUE)
	{
        switch(Test_step_counter)
        {
            case 0: Screen_welcome(); break;
            case 1: Screen_StartTest(); break;
            case 2: 
            case 3: 
            case 4:
            case 5: 
            case 6: 
            case 7: Screen_LEDTest(); break;
            case 8: Screen_LCDTest(); break;
            case 10: 
            case 11:
            case 12:
            case 13:
            case 14: Screen_KeyTest(); break;
            case 15: 
            case 16: 
            case 17: Screen_E2promTest(); break;
            case 19: Screen_BuzzerTest(); break;
            case 20: Screen_ADSample(); break;
            case 21: 
            case 22: 
            case 23: 
            case 24: 
            case 25: Screen_PwmOutput(); break;
            case 26: Screen_CANTransmitTest(); break;
            case 27: Screen_CANReceiveTest(); break;
            case 28: Screen_RtcTest(); break;
            case 29: Screen_Exit(); break;
            default : break;
        }
		LCD_refresh();
        task_status[UPDT_DISPLAY_ID] = ALIVE;
		osDelay(UPDT_DISPLAY_ITV);
	}
}  // end Display_task()   

/**
* @brief	display pin code input on the lcd
* @param	none
* @note     none
* @retval   none
*/
/*Display start test Screen on the LCD, It was showed at the first begining of start*/
void Screen_StartTest(void)
{
    Display_starttest_pic();
    put_string(2, 30, SIZE_10, "Device Info", HIDE_DISABLE);
    put_string(4, 0, SIZE_7, "HW ID:", HIDE_DISABLE);
    put_string(4, 42, SIZE_7, hardware_id, HIDE_DISABLE);
    put_string(5, 0, SIZE_7, "HW VER:", HIDE_DISABLE);
    Display_hw_version(5, 48, hardware_revision);
    put_string(6, 0, SIZE_7, "SN:", HIDE_DISABLE);
    put_string(6, 30, SIZE_7, disp_serial_number, HIDE_DISABLE);
}// end Screen_pincode() 

void Screen_LEDTest(void)
{
    LCD_ersea_area(0,0,128,64);
    Display_ledtest1_pic(Test_step_counter);  
}

void Screen_RtcTest(void)
{
    LCD_ersea_area(0,0,128,64);
    Display_Rtctest1_pic();  
}
void Screen_CANReceiveTest(void)
{
    LCD_ersea_area(0,0,128,64);
    Display_CANReceivetest1_pic();
}
void Screen_PwmOutput(void)
{
    LCD_ersea_area(0,0,128,64);
    Display_PWMtest1_pic(Test_step_counter);  
}

void Screen_CANTransmitTest(void)
{
    LCD_ersea_area(0,0,128,64);
    Display_CANTransmittest1_pic();  
}

void Screen_Exit(void)
{
    LCD_ersea_area(0,0,128,64);
    Display_exit1_pic();      
}
void Screen_BuzzerTest(void)
{
    LCD_ersea_area(0,0,128,64);
    Display_buzzer_pic();
}
void Screen_LCDTest(void)
{
    lcd_test_counter ++;
    LCD_ersea_area(0,0,128,64);
    if(lcd_test_counter < 5)
    {
        Display_lcdtest1_pic(1);
    }
    else if(lcd_test_counter < 10)
    {
        Display_lcdtest1_pic(2);
    }
    else if(lcd_test_counter < 15)
    {
        Display_lcdtest1_pic(3);
    }
    else if(lcd_test_counter < 20)
    {
        Display_lcdtest1_pic(4);
    }     
    else if(lcd_test_counter < 25)
    {
        Display_lcdtest1_pic(5);
        lcd_test_counter = 0;
        Test_step_counter ++;
    }    
}

void Screen_KeyTest(void)
{
    LCD_ersea_area(0,0,128,64);
    Display_keytest1_pic(Test_step_counter);
}

void Screen_E2promTest(void)
{
    LCD_ersea_area(0,0,128,64);
    if((Test_step_counter == 15) || (Test_step_counter == 16))
    {
        Display_e2promtest1_pic(Test_step_counter);
    }
    else
    {
        while(EEPROM_readback_flag != 0xff);
        if(EEPROM_test_val == 0xab)
        {
            Display_e2promtest1_pic(17);
            Test_step_counter ++;
        }
        else
        {
            Display_e2promtest1_pic(18);
        }
    }
}

void Screen_ADSample(void)
{
    LCD_ersea_area(0,0,128,64);
    Display_ADtest1_pic(ADC_VBATT);
}
/**
* @brief	Display Welcome image
* @param	none
* @note     none
* @retval   none
*/
void Screen_welcome(void)
{
    welcom_mode_counter ++;
    if(welcom_mode_counter <= 6)
    {
        Display_welcome_pic();     
    }
	else
    {
        welcom_mode_counter = 0;
        Test_step_counter ++;
        LCD_ersea_area(0,0,128,64);
    }
    
}// end Screen_welcome() 

/**
* @brief	Display Background Light Adjust Screen on the LCD, It was showed at the first begining of start
* @param	none
* @note     none
* @retval   none
*/
void Screen_maintence_info(void)
{
	u16 next_maintance_hour;
    next_maintance_hour = ServiceByHours / 10;
	proinfo_mode_counter ++;
	if(proinfo_mode_counter <= 8)
	{
	    show_hourmeter_number(4, Hrm_Struct.HM3_value);
        if(Language == LANGUAGE_EN)
        {
            put_string(6,34, SIZE_7, "Next service", HIDE_DISABLE);
        }
        else if(Language == LANGUAGE_CH)
        {
            Display_next_service_ch_pic();
        }
//	    put_string(7,49, SIZE_7, "1000 h", HIDE_DISABLE);
        show_service_hour(7, next_maintance_hour);
	}
	else
	{
		proinfo_mode_counter = 0;
		LCD_ersea_area(0,0,128,64);
        Init_flag = 1;
        if((Screen_Mode != MODE_ErrCode) || (Screen_Mode != MODE_ErrWarning))
        {
            Screen_Mode = MODE_ShowTime;
        }
	}
}

/**
* @brief	Display Background Light Adjust Screen on the LCD, It was showed at the first begining of start
* @param	none
* @note     none
* @retval   none
*/
void Screen_lighting(int bk_light_level)
{
//	lcd_adjust_lighting(bk_light_level, BK_LIGHT_ON);
//	display_adjust_bar( LIGHTING_BAR, bk_light_level, HIDE_DISABLE);
//	display_scrollbar(13);
//	display_hour_counter_sign(HIDE_DISABLE);
//	display_battary_col( 100 );
}// end Screen_lighting() 
/**
* @brief	Display oil lack 
* @param	none
* @note     none
* @retval   none
*/
void Screen_error_Maint(void)
{
    LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
    
	Display_battary_col(Current_Bat_Soc);
    if(Language == LANGUAGE_EN)
    {
        put_string(4,37, SIZE_7, "Service:", HIDE_DISABLE);
        put_string(5,28, SIZE_7, "maintenance", HIDE_DISABLE);
        put_string(6,22, SIZE_7, "planned today", HIDE_DISABLE);
    }
    else if(Language == LANGUAGE_CH)
    {
        Display_Maintance_ch();
    }  
    Display_scrollbar(BAR_SCREEN_OILERR);  //Draw scrollbar    
}
/**
* @brief	Display Battery low
* @param	none
* @note     none
* @retval   none
*/
void Screen_Bat_low(void)
{
    LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
    
	Display_battary_col(Current_Bat_Soc);
    if(Language == LANGUAGE_EN)
    {
        put_string(4,34, SIZE_7, "Caution !", HIDE_DISABLE);
        put_string(5,40, SIZE_7, "Battery", HIDE_DISABLE);
        put_string(6,31, SIZE_7, "discharged", HIDE_DISABLE);
    }
    else if(Language == LANGUAGE_CH)
    {
        Display_Batlow_ch();
    }  
    Display_scrollbar(BAR_SCREEN_OILERR);  //Draw scrollbar
}
/**
* @brief	Battary cut off level
* @param	none
* @note     none
* @retval   none
*/
void Screen_error_BatCutoff(void)
{
    u8 data_temp[2];
    char str1[40], a , b, str2[10];
    data_temp[0] = Current_Bat_Soc / 10;
    data_temp[1] = Current_Bat_Soc - data_temp[0] * 10;
    a = data_temp[0] + '0';
    b = data_temp[1] + '0';
    strcpy(str1, "over-discharged ! ");
    //strcat(str1, &a);
    //strcat(str1, &b);
    //strcat(str1, "%");
    if(a != '0')
    {
        str1[18] = a;
        str1[19] = b;
        str1[20] = '%';
        str2[0] = a;
        str2[1] = b;
        str2[2] = '%';
    }
    else
    {
        str1[18] = b;
        str1[19] = '%';
        str2[0] = b;
        str2[1] = '%';
    }
    
    LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
    
	Display_battary_col(Current_Bat_Soc);
    if(Language == LANGUAGE_EN)
    {
        put_string(4,37, SIZE_7, "Battery:", HIDE_DISABLE);
        put_string(5,0, SIZE_7, str1, HIDE_DISABLE);
        put_string(6,24, SIZE_7, "Restricted lift", HIDE_DISABLE);
        put_string(7,37, SIZE_7, "function", HIDE_DISABLE);
    }
    else if(Language == LANGUAGE_CH)
    {
        Display_BatteryCutOff_ch();
        put_string(4,85, SIZE_10, str2, HIDE_DISABLE);
    }  
    Display_scrollbar(BAR_SCREEN_OILERR);  //Draw scrollbar      
}
/**
* @brief	Display oil lack 
* @param	none
* @note     none
* @retval   none
*/
void Screen_error_Oil(void)
{
    LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
    
	Display_battary_col(Current_Bat_Soc);
    if(Language == LANGUAGE_EN)
    {
        put_string(4,28, SIZE_7, "Motor  fault:", HIDE_DISABLE);
        put_string(5,4, SIZE_7, "Please  restart  truck", HIDE_DISABLE);
    }
    else if(Language == LANGUAGE_CH)
    {
        Display_OilLack_ch();
    }  
    Display_scrollbar(BAR_SCREEN_OILERR);  //Draw scrollbar
}
/**
* @brief	Display the temp high screen 1
* @param	none
* @note     none
* @retval   none
*/
void Screen_error_Temp1(void)
{
    LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
    
	Display_battary_col(Current_Bat_Soc);
    if(Language == LANGUAGE_EN)
    {
        put_string(4,13, SIZE_7, "Motor  temperature:", HIDE_DISABLE);
        put_string(5,40, SIZE_7, "too  high", HIDE_DISABLE);
    }
    else if(Language == LANGUAGE_CH)
    {
        Display_MotoTemp_tooHigh1_ch();
    }  
    Display_scrollbar(BAR_SCREEN_OILERR);  //Draw scrollbar
}
void Screen_error_Temp2(void)
{
    LCD_ersea_area(0,0,128,64); //Clear the LCD buffer    
	Display_battary_col(Current_Bat_Soc);
    if(Language == LANGUAGE_EN)
    {
        put_string(4,2, SIZE_7, "Please  turn  off  truck", HIDE_DISABLE);
        put_string(5,16, SIZE_7, "for  a  few  moments", HIDE_DISABLE);
        put_string(6,37, SIZE_7, "and  reset", HIDE_DISABLE);
    }
    else if(Language == LANGUAGE_CH)
    {
        Display_MotoTemp_tooHigh2_ch();
    }      
    Display_scrollbar(BAR_SCREEN_TEMPERROR2);  //Draw scrollbar
}
/**
* @brief	Display the lift , track, steer and display error code while enter the initial menu
* @param	none
* @note     none
* @retval   none
*/
void Screen_error_code_init(void)
{
	U8 i, j;
	U16 TracK_ErrCode[4], Lift_ErrCode[4], Steer_ErrCode[4],  Disp_ErrCode[4] = {0};
	U16 TracK_ErrCode_DISP[4] = {0}, Lift_ErrCode_DISP[4] = {0}, Steer_ErrCode_Disp[4] = {0}, Disp_ErrCode_Disp[4] = {0};
	U8 Track_ErrPt = 0, Lift_ErrPt = 0, Steer_ErrPt = 0, Disp_ErrPt = 0;

	TracK_ErrCode[0] = Disp_TracErrCode[0];
	TracK_ErrCode[1] = Disp_TracErrCode[1];
	TracK_ErrCode[2] = Disp_TracErrCode[2];
	TracK_ErrCode[3] = Disp_TracErrCode[3];
	Lift_ErrCode[0] = Disp_LiftErrCode[0];
	Lift_ErrCode[1] = Disp_LiftErrCode[1];
	Lift_ErrCode[2] = Disp_LiftErrCode[2];
	Lift_ErrCode[3] = Disp_LiftErrCode[3];
	Steer_ErrCode[0] = Disp_SteerErrCode[0];
	Steer_ErrCode[1] = Disp_SteerErrCode[1];
	Steer_ErrCode[2] = Disp_SteerErrCode[2];
	Steer_ErrCode[3] = Disp_SteerErrCode[3];
	Disp_ErrCode[0] = Disp_DhsbErrCode[0];
	Disp_ErrCode[1] = Disp_DhsbErrCode[1];
	Disp_ErrCode[2] = Disp_DhsbErrCode[2];
	Disp_ErrCode[3] = Disp_DhsbErrCode[3];
	LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
	Display_scrollbar(BAR_SCREEN_INITERR);
	for(i = 0; i < 4; i ++)
	{
//	    LCD_draw_rectangle( 13 + 30 * i, 9, 5, 5, Black, HIDE_DISABLE);
		LCD_draw_line( 6 + 30 * i, 19, 21 + 30 * i, 19);
	}
    for(j = 0; j < 4; j ++)
	{
		if(TracK_ErrCode[j] > 200)
		{
			TracK_ErrCode_DISP[Track_ErrPt ++] = TracK_ErrCode[j];
		}
	}
    for(j = 0; j < 4; j ++)
	{
		if(Lift_ErrCode[j] > 200)
		{		
			Lift_ErrCode_DISP[Lift_ErrPt ++] = Lift_ErrCode[j];
		}
	}
	for(j = 0; j < 4; j ++)
	{
		if(Steer_ErrCode[j] > 200)
		{		
			Steer_ErrCode_Disp[Steer_ErrPt ++] = Steer_ErrCode[j];
		}
	}
	for(j = 0; j < 4; j ++)
	{
		if(Disp_ErrCode[j] > 200)
		{		
			Disp_ErrCode_Disp[Disp_ErrPt ++] = Disp_ErrCode[j];
		}
	}
	if(Track_ErrPt == 0)
	{
        LCD_draw_rectangle( 13, 9, 5, 5, Black, HIDE_DISABLE);
	}
	else
	{
		put_string(1, 12, SIZE_7, "T", HIDE_DISABLE);
	}
	for(i = 0; i < Track_ErrPt; i ++)
	{
	    LCD_draw_errorcode(4 + i, 4, TracK_ErrCode_DISP[i], HIDE_DISABLE);
	}
	for(i = Track_ErrPt; i < 4; i ++)
	{
		LCD_draw_line( 6, 35 + i * 8, 21, 35 + i * 8);			
	}	
	if(Lift_ErrPt == 0)
	{
        LCD_draw_rectangle( 43, 9, 5, 5, Black, HIDE_DISABLE);
	}
	else
	{
		put_string(1, 42, SIZE_7, "L", HIDE_DISABLE);
	}
	for(i = 0; i < Lift_ErrPt; i ++)
	{
		LCD_draw_errorcode(4 + i, 34, Lift_ErrCode_DISP[i], HIDE_DISABLE);
	}
	for(i = Lift_ErrPt; i < 4; i ++)
	{
		LCD_draw_line( 36, 35 + i * 8, 51, 35 + i * 8);			
	}		
	if(Steer_ErrPt == 0)
	{
        LCD_draw_rectangle( 73, 9, 5, 5, Black, HIDE_DISABLE);
	}
	else
	{
		put_string(1, 72, SIZE_7, "S", HIDE_DISABLE);
	}
	for(i = 0; i < Steer_ErrPt; i ++)
	{
		LCD_draw_errorcode(4 + i, 64, Steer_ErrCode_Disp[i], HIDE_DISABLE);
	}
	for(i = Steer_ErrPt; i < 4; i ++)
	{
		LCD_draw_line( 66, 35 + i * 8, 81, 35 + i * 8);			
	}	
	if(Disp_ErrPt == 0)
	{
        LCD_draw_rectangle( 103, 9, 5, 5, Black, HIDE_DISABLE);
	}
	else
	{
		put_string(1, 102, SIZE_7, "D", HIDE_DISABLE);
	}
	for(i = 0; i < Disp_ErrPt; i ++)
	{
		LCD_draw_errorcode(4 + i, 94, Disp_ErrCode_Disp[i], HIDE_DISABLE);
	}
	for(i = Disp_ErrPt; i < 4; i ++)
	{
		LCD_draw_line( 96, 35 + i * 8, 111, 35 + i * 8);			
	}	
}/*end of Screen_error_code()*/

/**
* @brief	Display the lift , track, steer and display error code while enter the initial menu
* @param	none
* @note     none
* @retval   none
*/
U8 error_dect(void)
{
	U8  j, error_type = ERROR_NONE;
	U16 TracK_ErrCode[4], Lift_ErrCode[4], Steer_ErrCode[4],  Disp_ErrCode[4] = {0};
	U8 Track_ErrPt = 0, Lift_ErrPt = 0, Steer_ErrPt = 0, Disp_ErrPt = 0;

	TracK_ErrCode[0] = Disp_TracErrCode[0];
	TracK_ErrCode[1] = Disp_TracErrCode[1];
	TracK_ErrCode[2] = Disp_TracErrCode[2];
	TracK_ErrCode[3] = Disp_TracErrCode[3];
	Lift_ErrCode[0] = Disp_LiftErrCode[0];
	Lift_ErrCode[1] = Disp_LiftErrCode[1];
	Lift_ErrCode[2] = Disp_LiftErrCode[2];
	Lift_ErrCode[3] = Disp_LiftErrCode[3];
	Steer_ErrCode[0] = Disp_SteerErrCode[0];
	Steer_ErrCode[1] = Disp_SteerErrCode[1];
	Steer_ErrCode[2] = Disp_SteerErrCode[2];
	Steer_ErrCode[3] = Disp_SteerErrCode[3];
	Disp_ErrCode[0] = Disp_DhsbErrCode[0];
	Disp_ErrCode[1] = 0x00;
	Disp_ErrCode[2] = 0x00;
	Disp_ErrCode[3] = 0x00;
    for(j = 0; j < 4; j ++)
	{
		if(TracK_ErrCode[j] > 200)
		{
			Track_ErrPt ++;
		}
	}
    for(j = 0; j < 4; j ++)
	{
		if(Lift_ErrCode[j] > 200)
		{		
			Lift_ErrPt ++;
		}
	}
	for(j = 0; j < 4; j ++)
	{
		if(Steer_ErrCode[j] > 200)
		{		
			Steer_ErrPt ++;
		}
	}
	for(j = 0; j < 4; j ++)
	{
		if(Disp_ErrCode[j] > 200)
		{		
			Disp_ErrPt ++;
		}
	}
    /*If the PDO oil lack is not exit, it will reset the oil lack warning display flag*/
    if(((BR1189_lift_status.pump_oil_level == OIL_OK) && (BR1189_trac_status.active_oil_level == OIL_OK)))
    {
        OilErrCodeDealFlag = TRUE;
        error_type = ERROR_OIL_LACK;
    }
    /*If the PDO high tempurature is not exit, it will reset thehigh tempurature display flag*/
    if((BR1189_lift_status.motor_over_tempurate == TEMPURATE_OK) && (BR1189_steer_status.motor_over_tempurate == TEMPURATE_OK)\
        && (BR1189_trac_status.motor_over_tempurate == TEMPURATE_OK) && (BR1189_lift_status.fets_over_tempurate == TEMPURATE_OK)\
        && (BR1189_steer_status.fets_over_tempurate == TEMPURATE_OK) && (BR1189_trac_status.fets_over_tempurate == TEMPURATE_OK))
    {
        TempurateErrCodeDealFlag = TRUE;
    }
    /*If do not need maintence, it will reset maintence display flag*/
    if(MatinInfoScreen == 0)
    {
        MaintErrCodeDealFlag = TRUE;
    }
    if(Current_Bat_Soc > CutOff)
    {
        BatCutoffDealFlag = TRUE;
    }
    if(Current_Bat_Soc > 10)
    {
        BatLowDealFlag = TRUE;
    }
    /*Select which error screen will be displayed*/
    if((Track_ErrPt > 0) || (Lift_ErrPt > 0) || (Steer_ErrPt > 0) || (Disp_ErrPt > 0))
    {
        error_type = ERROR_CODE_ACCUR;
        return error_type;
    }
    else if(((BR1189_lift_status.pump_oil_level == OIL_LACK) || (BR1189_trac_status.active_oil_level == OIL_LACK)) && (OilErrCodeDealFlag == TRUE))
    {
        error_type = ERROR_OIL_LACK;
        return error_type;
    }
    else if(((BR1189_lift_status.motor_over_tempurate == TEMPURATE_OVER) || (BR1189_steer_status.motor_over_tempurate == TEMPURATE_OVER)\
        || (BR1189_trac_status.motor_over_tempurate == TEMPURATE_OVER) || (BR1189_lift_status.fets_over_tempurate == TEMPURATE_OVER)\
        || (BR1189_steer_status.fets_over_tempurate == TEMPURATE_OVER) || (BR1189_trac_status.fets_over_tempurate == TEMPURATE_OVER)) && (TempurateErrCodeDealFlag == TRUE))
    {
        error_type = ERROR_OVER_TEMPURATE;
        return error_type;    
    }
    else if((MatinInfoScreen == 1) && (MaintErrCodeDealFlag == TRUE))
    {
        error_type = ERROR_MAINTENCE_WARNING;
        return error_type; 
    }
    else if((Current_Bat_Soc <= CutOff) && (BatCutoffDealFlag == TRUE))
    {
        error_type = ERROR_BATCUTOFF_WARNING;
        return error_type;
    }
    else if((Current_Bat_Soc <= 10) && (BatLowDealFlag == TRUE))
    {
        error_type = ERROR_BATT_LOW;
        return error_type;
    }
    else
    {
        error_type = ERROR_NONE;
        return error_type;       
    }

}/*end of Screen_error_code()*/

/**
* @brief	Display time LCD
* @param	none
* @note     none
* @retval   none
*/
void Screen_show_time(void)
{
	LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
	Display_scrollbar(BAR_SCREEN_TIME);  //Draw scrollbar
	Display_battary_col(Current_Bat_Soc);
	Display_data_time(sys_time, date_format);
}// end Screen_lighting() 

/**
* @brief	Display the work hours and the next service time hour
* @param	none
* @note     none
* @retval   none
*/
void Screen_show_hourmeter(void)
{
	u16 next_maintance_hour;
    next_maintance_hour = ServiceByHours / 10;
	LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
	
	Display_battary_col(Current_Bat_Soc);
//	put_string(4,49, SIZE_7, "0.0 h", HIDE_DISABLE);
    show_hourmeter_number(4, Hrm_Struct.HM3_value);
    if(Language == LANGUAGE_EN)
    {
        put_string(6,29, SIZE_7, "Next service", HIDE_DISABLE);
    }
    else if(Language == LANGUAGE_CH)
    {
        Display_next_service_ch_pic();
    }
    
//	put_string(7,45, SIZE_7, "1000 h", HIDE_DISABLE);
    show_service_hour(7, next_maintance_hour);
    Display_scrollbar(BAR_SCREEN_HOURMETER);  //Draw scrollbar
}/*end of Screen_show_hourmeter*/

/**
* @brief	Display the service information
* @param	none
* @note     none
* @retval   none
*/
void Screen_show_service(void)
{
    LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
    if(Err_Code_Exist_flag == TRUE)
    {
	    Display_scrollbar(BAR_SCREEN_SERVICE_WITH_ERR);  //Draw scrollbar
    }
    else
    {
        Display_scrollbar(BAR_SCREEN_SERVICE_NO_ERR);
    }
	Display_battary_col(Current_Bat_Soc);
	put_string(4,27, SIZE_7, "Address SAV", HIDE_DISABLE);
    put_string(5,8, SIZE_7, "After Sale Service", HIDE_DISABLE);
	put_string(6,37, SIZE_7, "Address", HIDE_DISABLE);
}
/**
* @brief	Display the lift , track, steer and display error code while enter the user menu
* @param	none
* @note     none
* @retval   none
*/
void Screen_error_code_user(void)
{
	U8 i, j;
	U16 TracK_ErrCode[4], Lift_ErrCode[4], Steer_ErrCode[4],  Disp_ErrCode[4] = {0};
	U16 TracK_ErrCode_DISP[4] = {0}, Lift_ErrCode_DISP[4] = {0}, Steer_ErrCode_Disp[4] = {0}, Disp_ErrCode_Disp[4] = {0};
	U8 Track_ErrPt = 0, Lift_ErrPt = 0, Steer_ErrPt = 0, Disp_ErrPt = 0;

	TracK_ErrCode[0] = Disp_TracErrCode[0];
	TracK_ErrCode[1] = Disp_TracErrCode[1];
	TracK_ErrCode[2] = Disp_TracErrCode[2];
	TracK_ErrCode[3] = Disp_TracErrCode[3];
	Lift_ErrCode[0] = Disp_LiftErrCode[0];
	Lift_ErrCode[1] = Disp_LiftErrCode[1];
	Lift_ErrCode[2] = Disp_LiftErrCode[2];
	Lift_ErrCode[3] = Disp_LiftErrCode[3];
	Steer_ErrCode[0] = Disp_SteerErrCode[0];
	Steer_ErrCode[1] = Disp_SteerErrCode[1];
	Steer_ErrCode[2] = Disp_SteerErrCode[2];
	Steer_ErrCode[3] = Disp_SteerErrCode[3];
	Disp_ErrCode[0] = 0x00;
	Disp_ErrCode[1] = 0x00;
	Disp_ErrCode[2] = 0x00;
	Disp_ErrCode[3] = 0x00;
	LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
	Display_scrollbar(BAR_SCREEN_USERERR);
	for(i = 0; i < 4; i ++)
	{
//	    LCD_draw_rectangle( 13 + 30 * i, 9, 5, 5, Black, HIDE_DISABLE);
		LCD_draw_line( 6 + 30 * i, 19, 21 + 30 * i, 19);
	}
    for(j = 0; j < 4; j ++)
	{
		if(TracK_ErrCode[j] > 200)
		{
			TracK_ErrCode_DISP[Track_ErrPt ++] = TracK_ErrCode[j];
		}
	}
    for(j = 0; j < 4; j ++)
	{
		if(Lift_ErrCode[j] > 200)
		{		
			Lift_ErrCode_DISP[Lift_ErrPt ++] = Lift_ErrCode[j];
		}
	}
	for(j = 0; j < 4; j ++)
	{
		if(Steer_ErrCode[j] > 200)
		{		
			Steer_ErrCode_Disp[Steer_ErrPt ++] = Steer_ErrCode[j];
		}
	}
	for(j = 0; j < 4; j ++)
	{
		if(Disp_ErrCode[j] > 200)
		{		
			Disp_ErrCode_Disp[Disp_ErrPt ++] = Disp_ErrCode[j];
		}
	}
	if(Track_ErrPt == 0)
	{
        LCD_draw_rectangle( 13, 9, 5, 5, Black, HIDE_DISABLE);
	}
	else
	{
		put_string(1, 12, SIZE_7, "T", HIDE_DISABLE);
	}
	for(i = 0; i < Track_ErrPt; i ++)
	{
	    LCD_draw_errorcode(4 + i, 4, TracK_ErrCode_DISP[i], HIDE_DISABLE);
	}
	for(i = Track_ErrPt; i < 4; i ++)
	{
		LCD_draw_line( 6, 35 + i * 8, 21, 35 + i * 8);			
	}	
	if(Lift_ErrPt == 0)
	{
        LCD_draw_rectangle( 43, 9, 5, 5, Black, HIDE_DISABLE);
	}
	else
	{
		put_string(1, 42, SIZE_7, "L", HIDE_DISABLE);
	}
	for(i = 0; i < Lift_ErrPt; i ++)
	{
		LCD_draw_errorcode(4 + i, 34, Lift_ErrCode_DISP[i], HIDE_DISABLE);
	}
	for(i = Lift_ErrPt; i < 4; i ++)
	{
		LCD_draw_line( 36, 35 + i * 8, 51, 35 + i * 8);			
	}		
	if(Steer_ErrPt == 0)
	{
        LCD_draw_rectangle( 73, 9, 5, 5, Black, HIDE_DISABLE);
	}
	else
	{
		put_string(1, 72, SIZE_7, "S", HIDE_DISABLE);
	}
	for(i = 0; i < Steer_ErrPt; i ++)
	{
		LCD_draw_errorcode(4 + i, 64, Steer_ErrCode_Disp[i], HIDE_DISABLE);
	}
	for(i = Steer_ErrPt; i < 4; i ++)
	{
		LCD_draw_line( 66, 35 + i * 8, 81, 35 + i * 8);			
	}	
	if(Disp_ErrPt == 0)
	{
        LCD_draw_rectangle( 103, 9, 5, 5, Black, HIDE_DISABLE);
	}
	else
	{
		put_string(1, 102, SIZE_7, "D", HIDE_DISABLE);
	}
	for(i = 0; i < Disp_ErrPt; i ++)
	{
		LCD_draw_errorcode(4 + i, 94, Disp_ErrCode_Disp[i], HIDE_DISABLE);
	}
	for(i = Disp_ErrPt; i < 4; i ++)
	{
		LCD_draw_line( 96, 35 + i * 8, 111, 35 + i * 8);			
	}	
}/*end of Screen_error_code()*/

/**
* @brief	Display the lightness adjust bar
* @param	none
* @note     none
* @retval   none
*/
void Screen_AdjustLightness(void)
{
	LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
	Display_scrollbar(BAR_SCREEN_ADJUST);
	Display_battary_col(Current_Bat_Soc);
	Display_adjust_bar( LIGHTING_BAR, LCD_Bklight , HIDE_DISABLE);
}/*end of Screen_AdjustLightness()*/
/**
* @brief	Display Error Delete dialog
* @param	none
* @note     none
* @retval   none
*/
void Screen_ErrorDelet_Dialog(void)
{
    LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
   
    Display_battary_col(Current_Bat_Soc);
    if(Language == LANGUAGE_EN)
    {
        put_string(4,46, SIZE_7, "Delete", HIDE_DISABLE);
        put_string(5,28, SIZE_7, "this  warning  ?", HIDE_DISABLE);  
        put_string(6,49, SIZE_7, "+  Yes", HIDE_DISABLE);       
        put_string(7,52, SIZE_7, "-  No", HIDE_DISABLE);    
    }
    else if(Language == LANGUAGE_CH)
    {
        Display_Confirm_Delete_ch();
    }
    Display_scrollbar(BAR_SCREEN_DELETE_WARNING);
}

void err_mode_detect(void)
{
    u8 Err_det;
    Err_det = error_dect();                  //if detect the error code, then show the error code
    /*If error code exit and larger than 200, the error code display will be trigger*/
    if(Err_det == ERROR_CODE_ACCUR)
    {
        if(Err_Code_Hide_flag == FALSE)
        {
            Screen_Mode = MODE_InitErrCode;
            Err_Code_Exist_flag = TRUE;
        }   
        if(Err_Batlow_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Batlow_Exist_flag = FALSE;
            Err_Batlow_Hide_flag = FALSE;
        }       
        if(Err_BatCutoff_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_BatCutoff_Exist_flag = FALSE;
            Err_BatCutoff_Hide_flag = FALSE;            
        }
        if(Err_Oil_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Oil_Exist_flag = FALSE;
            Err_Oil_Hide_flag = FALSE;
        }
        if(Err_Temp_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Temp_Exist_flag = FALSE;
            Err_Temp_Hide_flag = FALSE;
        }    
        if(Err_Maint_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Maint_Exist_flag = FALSE;
            Err_Maint_Hide_flag = FALSE;
        }          
    }
    else if(Err_det == ERROR_BATT_LOW)
    {
        if(Err_Batlow_Hide_flag == FALSE)
        {
            Screen_Mode = Mode_BatLow;
            Err_Batlow_Exist_flag = TRUE;
        }
        if(Err_Code_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Code_Exist_flag = FALSE;
            Err_Code_Hide_flag = FALSE;
        }     
        if(Err_BatCutoff_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_BatCutoff_Exist_flag = FALSE;
            Err_BatCutoff_Hide_flag = FALSE;            
        }
        if(Err_Oil_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Oil_Exist_flag = FALSE;
            Err_Oil_Hide_flag = FALSE;
        }
        if(Err_Temp_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Temp_Exist_flag = FALSE;
            Err_Temp_Hide_flag = FALSE;
        }    
        if(Err_Maint_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Maint_Exist_flag = FALSE;
            Err_Maint_Hide_flag = FALSE;
        }     

    }
    /*If the battary is lower than the cut off column*/
    else if(Err_det == ERROR_BATCUTOFF_WARNING)
    {
        if(Err_BatCutoff_Hide_flag == FALSE)
        {
            Screen_Mode = Mode_BatCutoff;
            Err_BatCutoff_Exist_flag = TRUE;
        }
        if(Err_Code_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Code_Exist_flag = FALSE;
            Err_Code_Hide_flag = FALSE;
        }  
        if(Err_Batlow_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Batlow_Exist_flag = FALSE;
            Err_Batlow_Hide_flag = FALSE;
        }          
        if(Err_Oil_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Oil_Exist_flag = FALSE;
            Err_Oil_Hide_flag = FALSE;
        }  
        if(Err_Temp_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Temp_Exist_flag = FALSE;
            Err_Temp_Hide_flag = FALSE;
        }   
        if(Err_Maint_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Maint_Exist_flag = FALSE;
            Err_Maint_Hide_flag = FALSE;
        }     
    }
    /*If PDO motor oil lack exit, the motor oil lack display will be trigger*/
    else if(Err_det == ERROR_OIL_LACK)
    {
        if(Err_Oil_Hide_flag == FALSE)
        {
            Screen_Mode = MODE_OilErrCode;
            Err_Oil_Exist_flag = TRUE;
        }                
        if(Err_Code_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Code_Exist_flag = FALSE;
            Err_Code_Hide_flag = FALSE;
        }     

        if(Err_BatCutoff_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_BatCutoff_Exist_flag = FALSE;
            Err_BatCutoff_Hide_flag = FALSE;            
        }
        if(Err_Temp_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Temp_Exist_flag = FALSE;
            Err_Temp_Hide_flag = FALSE;
        }   
        if(Err_Maint_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Maint_Exist_flag = FALSE;
            Err_Maint_Hide_flag = FALSE;
        }                 
    }
    /*If PDO high tempurature exit, the high tempurature display will be trigger*/
    else if(Err_det == ERROR_OVER_TEMPURATE)
    {
        if(Err_Temp_Hide_flag == FALSE)
        {
            Screen_Mode = MODE_TempErrCode;
            Err_Temp_Exist_flag = TRUE;
        }    
        if(Err_Code_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Code_Exist_flag = FALSE;
            Err_Code_Hide_flag = FALSE;
        }
        if(Err_Batlow_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Batlow_Exist_flag = FALSE;
            Err_Batlow_Hide_flag = FALSE;
        }       
        if(Err_BatCutoff_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_BatCutoff_Exist_flag = FALSE;
            Err_BatCutoff_Hide_flag = FALSE;            
        }  
        if(Err_Oil_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Oil_Exist_flag = FALSE;
            Err_Oil_Hide_flag = FALSE;
        }  
        if(Err_Maint_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Maint_Exist_flag = FALSE;
            Err_Maint_Hide_flag = FALSE;
        }            
    }
    /*If hourmeter lager than set hours, the maintence display will be trigger*/
    else if(Err_det == ERROR_MAINTENCE_WARNING)
    {
        if(Err_Maint_Hide_flag == FALSE)
        {
            Screen_Mode = MODE_MaintErrCode;
            Err_Maint_Exist_flag = TRUE;
        }   
        if(Err_Code_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Code_Exist_flag = FALSE;
            Err_Code_Hide_flag = FALSE;
        }  
        if(Err_Batlow_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Batlow_Exist_flag = FALSE;
            Err_Batlow_Hide_flag = FALSE;
        }       
        if(Err_BatCutoff_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_BatCutoff_Exist_flag = FALSE;
            Err_BatCutoff_Hide_flag = FALSE;            
        }
        if(Err_Oil_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Oil_Exist_flag = FALSE;
            Err_Oil_Hide_flag = FALSE;
        }   
        if(Err_Temp_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Temp_Exist_flag = FALSE;
            Err_Temp_Hide_flag = FALSE;
        }       
    }
    else if(Err_det == ERROR_NONE)           //if not any error code, then exit
    {
        if(Err_Code_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Code_Exist_flag = FALSE;
            Err_Code_Hide_flag = FALSE;
        }
        if(Err_Batlow_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Batlow_Exist_flag = FALSE;
            Err_Batlow_Hide_flag = FALSE;
        }       
        if(Err_BatCutoff_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_BatCutoff_Exist_flag = FALSE;
            Err_BatCutoff_Hide_flag = FALSE;            
        }
        if(Err_Oil_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Oil_Exist_flag = FALSE;
            Err_Oil_Hide_flag = FALSE;
        }
        if(Err_Temp_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Temp_Exist_flag = FALSE;
            Err_Temp_Hide_flag = FALSE;
        }
        if(Err_Maint_Exist_flag == TRUE)
        {
            Screen_Mode = MODE_ShowTime;
            Err_Maint_Exist_flag = FALSE;
            Err_Maint_Hide_flag = FALSE;
        }
    }
}
/**
* @brief	Display admin pincode input screen
* @param	none
* @note     none
* @retval   none
*/
void Screen_admin_pincode(void)
{
    u8 master_pincode[4], i;
    LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
	
    LCD_ersea_area(0,0,128,64);
    if(Language == LANGUAGE_EN)
    {
        put_string(1, 43, SIZE_10, "Enter", HIDE_DISABLE);
        put_string(3, 19, SIZE_10, "Master Code", HIDE_DISABLE);
    }
    else if(Language == LANGUAGE_CH)
    {
        Display_admin_pincode_ch_pic();
    }
	Display_scrollbar(BAR_SCREEN_PINCOD);
	switch(Pin_Code.PinCode_Pt)
	{
		case 0 : put_string(6, 32, SIZE_10, "- - - -", HIDE_DISABLE); break;
		case 1 : put_string(6, 32, SIZE_10, "* - - -", HIDE_DISABLE); break;
		case 2 : put_string(6, 32, SIZE_10, "* * - -", HIDE_DISABLE); break;
		case 3 : put_string(6, 32, SIZE_10, "* * * -", HIDE_DISABLE); break;
		case 4 : put_string(6, 32, SIZE_10, "* * * *", HIDE_DISABLE); break;
		default: break;
	}
    /*check if the pin code is correct*/
	if(Pin_Code.PinCode_Pt >= 4)
	{
        master_pincode[3] = DigicodeAdministrator & 0x000f;
        master_pincode[2] = (DigicodeAdministrator >> 4) & 0x000f;
        master_pincode[1] = (DigicodeAdministrator >> 8) & 0x000f;
        master_pincode[0] = (DigicodeAdministrator >> 12) & 0x000f;
        if((master_pincode[3] == Pin_Code.PinCodeNum[3]) && (master_pincode[2] == Pin_Code.PinCodeNum[2]) \
            && (master_pincode[1] == Pin_Code.PinCodeNum[1]) && (master_pincode[0] == Pin_Code.PinCodeNum[0]))
        {
            Screen_Mode = MODE_ResetPincode;
            for(i = 0; i < 4; i ++)
		    {
			    Pin_Code.PinCodeNum[i] = 0;
			    Pin_Code.PinCode_Pt = 0;
		    }
        }
        else
	    {
		    for(i = 0; i < 4; i ++)
		    {
			    Pin_Code.PinCodeNum[i] = 0;
			    Pin_Code.PinCode_Pt = 0;
		    }
	    }
	}
}

void Screen_Pincode_reset(void)
{

    LCD_ersea_area(0,0,128,64); //Clear the LCD buffer
    if(Language == LANGUAGE_EN)
    {
        put_string(1, 15, SIZE_10, "Please input", HIDE_DISABLE);
        put_string(3, 8, SIZE_10, "a new pin code", HIDE_DISABLE);
    }
    else if(Language == LANGUAGE_CH)
    {
        Display_enter_pincode_ch_pic();
    }
    Display_scrollbar(BAR_SCREEN_PINCOD);
    switch(Pin_Code.PinCode_Pt)
	{
		case 0 : put_string(6, 32, SIZE_10, "- - - -", HIDE_DISABLE); break;
		case 1 : put_string(6, 32, SIZE_10, "* - - -", HIDE_DISABLE); break;
		case 2 : put_string(6, 32, SIZE_10, "* * - -", HIDE_DISABLE); break;
		case 3 : put_string(6, 32, SIZE_10, "* * * -", HIDE_DISABLE); break;
		case 4 : put_string(6, 32, SIZE_10, "* * * *", HIDE_DISABLE); break;
		default: break;
	}
    if(Pin_Code.PinCode_Pt >= 4)
	{
        Screen_Mode = MODE_DigiCodeEnsure;
    }
}
/**
* @brief	Display Error Delete dialog
* @param	none
* @note     none
* @retval   none
*/
void Screen_DigiCodeEnsure_Dialog(void)
{
    char code_temp[4];
    code_temp[0] = Pin_Code.PinCodeNum[0] + '0';
    code_temp[1] = Pin_Code.PinCodeNum[1] + '0';
    code_temp[2] = Pin_Code.PinCodeNum[2] + '0';
    code_temp[3] = Pin_Code.PinCodeNum[3] + '0';
    LCD_ersea_area(0,0,128,64); //Clear the LCD buffer

    if(Language == LANGUAGE_EN)
    {
        put_string(0,14, SIZE_10, "Add pin code", HIDE_DISABLE);
        put_string(2,46, SIZE_10, code_temp, HIDE_DISABLE);
        put_string(5,52, SIZE_7, "OK  ?", HIDE_DISABLE);  
        put_string(6,49, SIZE_7, "+  Yes", HIDE_DISABLE);       
        put_string(7,52, SIZE_7, "-  No", HIDE_DISABLE);    
    }
    else if(Language == LANGUAGE_CH)
    {
        Display_NewPincode_ch();
        put_string(2,46, SIZE_10, code_temp, HIDE_DISABLE);
        Display_Confirm_AddPincode_ch();
    }
    Display_scrollbar(BAR_SCREEN_DELETE_WARNING);
}

